import { isUndefined } from "mmb";
import { AppCTX } from "../../types/koaExtend";
import { createBrandApiMWRouter } from "../../middleware/brand-api/brandAdminChecker";
import pageMW from "../../middleware/pager";
const router = createBrandApiMWRouter();
import {
  deliverOrder,
  prepareOrder,
  pickOrderByB,
  listOrderItemsForB,
  getOrderBySerialNumber,
} from "../../service/orderService";
import { isSearchTerm } from "../../bg-enums/searchTerm";
import OrderStatus, { isOrderStatus } from "../../bg-enums/orderStatus";

// 订单确认备货
router.put(
  "/brd/:brandId/order/:orderSerialNumber/prepare",
  async (ctx: AppCTX, next) => {
    const { brandId, orderSerialNumber } = ctx.params;
    const { userId } = ctx;
    const prepareRet = await prepareOrder({ orderSerialNumber, brandId });
    ctx.body = prepareRet;
  }
);

// 订单确认提货
router.put(
  "/brd/:brandId/order/:orderSerialNumber/pick",
  async (ctx: AppCTX, next) => {
    const { brandId, orderSerialNumber } = ctx.params;
    const { userId } = ctx;
    const { pickupCode } = ctx.request.body;
    const pickRet = await pickOrderByB({
      orderSerialNumber,
      brandId,
      pickupCode,
    });
    ctx.body = pickRet;
  }
);

// 订单确认发货
router.put(
  "/brd/:brandId/order/:orderSerialNumber/deliver",
  async (ctx: AppCTX, next) => {
    const { brandId, orderSerialNumber } = ctx.params;
    const { platform, platformOrderNum } = ctx.request.body;
    const { userId } = ctx;
    const deliverRet = await deliverOrder({
      orderSerialNumber,
      brandId,
      platform,
      platformOrderNum,
    });
    ctx.body = deliverRet;
  }
);

// 获取品牌售出订单
router.get("/brd/:brandId/orders", pageMW, async (ctx: AppCTX, next) => {
  const { brandId } = ctx.params;
  const { searchTerm, searchValue, isAll, orderStatus } = ctx.query;
  const { userId, limit, offset, ts } = ctx; // 这个是操作人
  let arrOrderStatus: OrderStatus[] = [];
  if (!isUndefined(orderStatus)) {
    for (let ps of orderStatus.split(",")) {
      if (!isOrderStatus(ps)) {
        ctx.throw(400, "orderStatus invalid");
      }
      arrOrderStatus.push(ps);
    }
  }
  if (!isUndefined(searchTerm) && !isSearchTerm(searchTerm)) {
    ctx.throw(400, "invalid searchTerm");
  }

  const { total, items } = await listOrderItemsForB(
    {
      brandId,
      isAll: isAll === "true",
      orderStatuses: arrOrderStatus,
      searchTerm,
      searchValue,
    },
    { limit, offset, ts }
  );
  ctx.body = {
    total,
    rows: items,
  };
});

// 获取订单详情(品牌管理视角)
router.get(
  "/brd/:brandId/order/:orderSerialNumber",
  async (ctx: AppCTX, next) => {
    const { brandId, orderSerialNumber } = ctx.params;
    const { userId } = ctx;

    const order = await getOrderBySerialNumber(orderSerialNumber);
    if (isUndefined(order)) {
      ctx.throw(400, "invalid orderSerialNumber");
    }
    if (order.brandId !== brandId) {
      ctx.throw(403, "no permit access to the order");
    }
    // switch (order.orderStatus) {
    //   case 'created':
    //     break;
    //   case 'pending':
    //     break;
    //   case 'payed':
    //     break;
    //   case 'delivered':
    //     break;
    //   case 'prepared':
    //     break;
    //   case 'finished':
    //     break;
    //   case 'before_pay_closed':
    //     break;
    //     case 'after_pay_closed':
    //     break;
    //   default:
    //     break;
    // }

    ctx.body = order;
  }
);

export default router;
